using System;
using System.Collections.Generic;
using System.Text;
using OgilvyOne.IDAL;
using System.Data.SqlClient;
using System.Data;
using OgilvyOne.Model;
using OgilvyOne.Utility;

namespace OgilvyOne.SqlServerDAL
{
    public class Privilege : IPrivilege
    {
        // Fields
        private const string PARM_MENU_UID = "@Menu_ID";
        private const string PARM_ROLE_UID = "@Role_ID";
        private const string PARM_USER_UID = "@User_ID";
        private const string SQL_SELECT_BY_ROLE = " SELECT privilege_value FROM system_privilege where role_uid = @Role_ID and menu_uid = @Menu_ID ";
        private const string SQL_SELECT_BY_USER = " SELECT privilege.privilege_value FROM system_privilege privilege INNER JOIN system_role_user role ON  privilege.role_uid = role.role_uid and role.user_uid = @User_ID and privilege.menu_uid = @Menu_ID ";

        // Methods
        public int getPrivilegeValue(int iRoleID, int iMenuID)
        {
            int num = 1;
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@Role_ID", SqlDbType.Int, 4, "role_uid"), new SqlParameter("@Menu_ID", SqlDbType.Int, 4, "menu_uid") };
            cmdParms[0].Value = iRoleID;
            cmdParms[1].Value = iMenuID;
            using (SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, " SELECT privilege_value FROM system_privilege where role_uid = @Role_ID and menu_uid = @Menu_ID ", cmdParms))
            {
                while (reader.Read())
                {
                    num = reader.GetInt32(0);
                }
            }
            return num;
        }

        public int getValueByUserID(int iUserID, int iMenuID)
        {
            int num = 1;
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@User_ID", SqlDbType.Int, 4, "user_uid"), new SqlParameter("@Menu_ID", SqlDbType.Int, 4, "menu_uid") };
            cmdParms[0].Value = iUserID;
            cmdParms[1].Value = iMenuID;
            using (SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, " SELECT privilege.privilege_value FROM system_privilege privilege INNER JOIN system_role_user role ON  privilege.role_uid = role.role_uid and role.user_uid = @User_ID and privilege.menu_uid = @Menu_ID ", cmdParms))
            {
                while (reader.Read())
                {
                    num = reader.GetInt32(0);
                }
            }
            return num;
        }

        public int Insert(PrivilegeInfo newInfo)
        {
            int roleID = newInfo.RoleID;
            string menuKeyStr = newInfo.MenuKeyStr;
            string cmdText = " delete from system_privilege where role_uid=" + roleID.ToString() + ";";
            string str3 = ",";
            string[] strArray = menuKeyStr.Split(new char[] { ',' });
            foreach (string str4 in strArray)
            {
                string str6;
                string[] strArray2 = str4.Split(new char[] { '_' });
                if (str3.IndexOf("," + strArray2[0] + ",") > -1)
                {
                    str6 = cmdText;
                    cmdText = str6 + " update system_privilege set privilege_value = privilege_value*" + strArray2[1] + "  where role_uid = " + roleID.ToString() + " and menu_uid= " + strArray2[0] + "; ";
                }
                else
                {
                    str3 = str3 + strArray2[0] + ",";
                    str6 = cmdText;
                    cmdText = str6 + " insert into system_privilege values ( " + roleID.ToString() + "," + strArray2[0] + "," + strArray2[1] + " );";
                }
            }
            int num2 = 0;
            using (SqlConnection connection = new SqlConnection(SQLHelper.CONN_STRING_NON_DTC))
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        num2 = SQLHelper.ExecuteNonQuery(transaction, CommandType.Text, cmdText, null);
                        transaction.Commit();
                    }
                    catch (SqlException exception)
                    {
                        transaction.Rollback();
                        string sMethod = "OgilvyOne.SqlServerDAL.Privilege.Insert(PrivilegeInfo newInfo)";
                        EventsLog.WriteLog(exception, sMethod);
                    }
                    return num2;
                }
            }
        }
    }
}
